package secretary;

import home.HomePage;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.StudentInformation;

public class SeniorsList extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	
	
	public SeniorsList () throws SQLException, IOException
	{
		
		add(new BookmarkablePageLink("home", SecretaryHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("freshmen", FreshmenList.class));
		add(new BookmarkablePageLink("sophies", SophiesList.class));
		add(new BookmarkablePageLink("juniors", JuniorsList.class));
		add(new BookmarkablePageLink("seniors", SeniorsList.class));
		add(new BookmarkablePageLink("add", AddStudentAccount.class));
		
		Form form = new Form("seniorsListForm");
		
		
		
		List<StudentInformation> list = (List<StudentInformation>)sqlMap.queryForList("StudentInformation.StudentInformation", "4");
		
		ListView eachSenior = new ListView("eachSenior", list) {
			private static final long serialVersionUID = 1L;
			
			protected void populateItem(ListItem item) {
				final StudentInformation si = (StudentInformation) item.getModelObject();
				item.add(new CustomCheckBox("checkbox", new CustomCheckBoxModel(si.getStudent_id())));
				
				item.add(new Label("studentId", si.getStudent_id()));
				String firstName = si.getStudent_firstName();
				String middleName = si.getStudent_middleName();
				System.out.println(middleName);
				String lastName = si.getStudent_lastName();
				System.out.println(lastName);
				String fullName = firstName + " " + middleName + " " + lastName; 
				item.add(new Label("name", fullName));
						}
		};
		
		
		form.add(eachSenior);
		
		
		
		add(form);
	}
	
	public class CustomCheckBox extends CheckBox{
		private static final long serialVersionUID = 1L;

		public CustomCheckBox(String id, IModel model) {
			super(id, model);
		}
		
		protected boolean wantOnSelectionChangedNotifications() {
			return false;
		}
	}

	private final List<String> selections = new ArrayList();
	
	public class CustomCheckBoxModel implements IModel, Serializable{
		private static final long serialVersionUID = 1L;
		private final String id;
		
		public CustomCheckBoxModel(String id) {
			super();
			this.id = id;
		}

		public IModel getNestedModel() {
			return null;
		}

		public Object getObject(){
			return isSelected();
		}
		
		private Boolean isSelected() {
			if (selections.contains(id))
				return true;
			else
				return false;
		}
			
		public void setObject(Object object){
			boolean selected = ((Boolean) object).booleanValue();
			boolean previouslySelected = isSelected();
			if (selected){
				if (!previouslySelected) {
					selections.add((String)id);
				}
			} else {
				if (previouslySelected) {
					selections.remove((String)id);
				}
			}
		}
		public void detach() {}
	}


}
